import axios from 'axios'
import * as vscode from 'vscode'
import { twsManager } from '../TwsManager';
import BASE_URL from '../contstants/base-url'

axios.defaults.baseURL = BASE_URL
axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*'
axios.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8'

export const getProgramsTopicsTasks = async (jwt: string, sessionId: string) => {
  try {
    const res = await axios.get('/program-center/api/programs/topics/tasks', {
      headers: {
        token: jwt,
        sessionId,
        id: '21'
      }
    })
    return res.data
  } catch (e) {
    vscode.window.showErrorMessage(e.message);
  }
}
export const login = async (user: any): Promise<any> => {
  try {
    return await axios.post('/api/auth/username', user)
  } catch (e) {
    vscode.window.showErrorMessage(e.message);
  }

}

export const getAssignments = async (taskId: number, jwt: string, sessionId: string) => {
  try {
    const res = await axios.get(`/program-center/api/v2/tasks/${taskId}/assignments`, {
      headers: {
        token: jwt,
        sessionId,
        id: '21'
      }
    })
    return res.data
  } catch (e) {
    vscode.window.showErrorMessage(e.message);
  }
}

export const getAssignment = async (assignmentId: number, jwt: string, sessionId: string) => {
  try {
    const res = await axios.get(`/program-center/api/v2/assignments/${assignmentId}/quizzes`, {
      headers: {
        token: jwt,
        sessionId,
        id: '21'
      }
    })
    return res.data[0] || {}
  } catch (e) {
    vscode.window.showErrorMessage(e.message);
  }
}
export const startAssignment = async (taskId: number, assignmentId: number, userId: number, jwt: string, sessionId: string) => {
  try {
    await axios.post(`/program-center/api/v2/tasks/assignments/users`, { taskId, assignmentId, userId }, {
      headers: {
        token: jwt,
        sessionId,
        id: '21'
      }
    })
  } catch (e) {
    vscode.window.showErrorMessage(e.message);

  }
}
export const submitSingleOnlineCoding = async (data: any, loadLogs: Function) => {
  const { jwt, sessionId, id = 21 } = twsManager.getUser()
  try {
    const res = await axios.post(`/program-center/api/v2/onlineLanguageQuizzes/submission`, data,
      {
        headers: {
          token: jwt,
          sessionId,
          id
        }
      })
    loadLogs(res.data.id)
  } catch (e) {
    vscode.window.showErrorMessage(e.message);
  }
}

export const getSingleOnlineCodingRunningLogs = async (taskId: number, assignmentId: number, quizId: number, submissionId: number, getResult: Function) => {
  const { jwt, sessionId, id = 21 } = twsManager.getUser()
  try {
    const res = await axios.get(`/program-center/api/v2/onlineLanguageQuizzes/submissions/${submissionId}/students/tasks/${taskId}/assignments/${assignmentId}/quizzes/${quizId}/runningLog`, {
      headers: {
        token: jwt,
        sessionId,
        id
      }
    })
    if (getResult) {
      getResult(res.data)
    }
  } catch (e) {
    vscode.window.showErrorMessage(e.message);
    return
  }
}